home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import sys
- import time
- import socket
- import select
- import struct
- import random
- import re
- import cStringIO
- from g import *
- import utils
- MAX_ANSWERS_PER_PACKET = 24
- QTYPE_A = 1
- QTYPE_TXT = 16
- QTYPE_SRV = 33
- QTYPE_AAAA = 28
- QTYPE_PTR = 12
- QCLASS_IN = 1
-
- def read_utf8(offset, data, l):
- return (offset + l, data[offset:offset + l].decode('utf-8'))
-
-
- def read_data(offset, data, l):
- return (offset + l, data[offset:offset + l])
-
-
- def read_data_unpack(offset, data, fmt):
- l = struct.calcsize(fmt)
- return (offset + l, struct.unpack(fmt, data[offset:offset + l]))
-
-
- def read_name(offset, data):
- result = ''
- off = offset
- next = -1
- first = off
- while True:
- l = ord(data[off])
- off += 1
- if l == 0:
- break
-
- t = l & 192
- if t == 0:
- (off, utf8) = read_utf8(off, data, l)
- result = ''.join([
- result,
- utf8,
- '.'])
- continue
- if t == 192:
- if next < 0:
- next = off + 1
-
- off = (l & 63) << 8 | ord(data[off])
- if off >= first:
- log.error('Bad domain name (circular) at 0x%04x' % off)
- break
-
- first = off
- continue
- log.error('Bad domain name at 0x%04x' % off)
- break
- if next >= 0:
- offset = next
- else:
- offset = off
- return (offset, result)
-
-
- def write_name(packet, name):
- for p in name.split('.'):
- utf8_string = p.encode('utf-8')
- packet.write(struct.pack('!B', len(utf8_string)))
- packet.write(utf8_string)
-
-
-
- def create_outgoing_packets(answers):
- index = 0
- num_questions = 1
- first_packet = True
- packets = []
- packet = cStringIO.StringIO()
- answer_record = cStringIO.StringIO()
- while True:
- packet.seek(0)
- packet.truncate()
- num_answers = len(answers[index:index + MAX_ANSWERS_PER_PACKET])
- if num_answers == 0 and num_questions == 0:
- break
-
- flags = 512
- if len(answers) - index <= MAX_ANSWERS_PER_PACKET:
- flags = 0
-
- packet.write(struct.pack('!HHHHHH', 0, flags, num_questions, num_answers, 0, 0))
- if num_questions:
- write_name(packet, '_pdl-datastream._tcp.local')
- packet.write(struct.pack('!B', 0))
- packet.write(struct.pack('!HH', QTYPE_PTR, QCLASS_IN))
-
- first_record = True
- for d in answers[index:index + MAX_ANSWERS_PER_PACKET]:
- answer_record.seek(0)
- answer_record.truncate()
- if not first_packet and first_record:
- first_record = False
- write_name(answer_record, '_pdl-datastream._tcp.local')
- answer_record.write(struct.pack('!B', 0))
- else:
- answer_record.write(struct.pack('!H', 49164))
- answer_record.write(struct.pack('!HH', QTYPE_PTR, QCLASS_IN))
- answer_record.write(struct.pack('!I', 65535))
- rdlength_pos = answer_record.tell()
- answer_record.write(struct.pack('!H', 0))
- write_name(answer_record, d)
- answer_record.write(struct.pack('!H', 49164))
- rdlength = answer_record.tell() - rdlength_pos - 2
- answer_record.seek(rdlength_pos)
- answer_record.write(struct.pack('!H', rdlength))
- answer_record.seek(0)
- packet.write(answer_record.read())
-
- packets.append(packet.getvalue())
- index += 20
- if first_packet:
- num_questions = 0
- first_packet = False
- continue
- return packets
-
-
- def detectNetworkDevices(ttl = 4, timeout = 10):
- (mcast_addr, mcast_port) = ('224.0.0.251', 5353)
- found_devices = { }
- answers = []
-
- try:
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
- x = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- x.connect(('1.2.3.4', 56))
- intf = x.getsockname()[0]
- x.close()
- s.setblocking(0)
- ttl = struct.pack('B', ttl)
- except socket.error:
- log.error('Network error')
- return { }
-
-
- try:
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
- except (AttributeError, socket.error):
- pass
-
-
- try:
- s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, ttl)
- s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf) + socket.inet_aton('0.0.0.0'))
- s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)
- except Exception:
- e = None
- log.error('Unable to setup multicast socket for mDNS: %s' % e)
- return { }
-
- now = time.time()
- next = now
- last = now + timeout
- delay = 1
- while True:
- now = time.time()
- if now > last:
- break
-
- if now >= next:
-
- try:
- for p in create_outgoing_packets(answers):
- log.debug('Outgoing: (%d)' % len(p))
- log.log_data(p, width = 16)
- s.sendto(p, 0, (mcast_addr, mcast_port))
- except socket.error:
- e = None
- log.error('Unable to send broadcast DNS packet: %s' % e)
-
- next += delay
- delay *= 2
-
- update_spinner()
- (r, w, e) = select.select([
- s], [], [
- s], 0.5)
- if not r:
- continue
-
- (data, addr) = s.recvfrom(16384)
- if data:
- update_spinner()
- y = {
- 'num_devices': 1,
- 'num_ports': 1,
- 'product_id': '',
- 'mac': '',
- 'status_code': 0,
- 'device2': '0',
- 'device3': '0',
- 'note': '' }
- log.debug('Incoming: (%d)' % len(data))
- log.log_data(data, width = 16)
- offset = 0
- (id, flags, num_questions, num_answers, num_authorities, num_additionals) = (offset,)
- log.debug('Response: ID=%d FLAGS=0x%x Q=%d A=%d AUTH=%d ADD=%d' % (id, flags, num_questions, num_answers, num_authorities, num_additionals))
- for question in range(num_questions):
- update_spinner()
- (offset, name) = read_name(offset, data)
- (typ, cls) = (offset,)
- log.debug('Q: %s TYPE=%d CLASS=%d' % (name, typ, cls))
-
- fmt = '!HHiH'
- for record in range(num_answers + num_authorities + num_additionals):
- update_spinner()
- (offset, name) = read_name(offset, data)
- (offset, info) = read_data_unpack(offset, data, '!HHiH')
- if info[0] == QTYPE_A:
- (offset, result) = read_data(offset, data, 4)
- ip = []([ str(ord(x)) for x in result ])
- log.debug('A: %s' % ip)
- y['ip'] = ip
- continue
- []
- if info[0] == QTYPE_PTR:
- (offset, name) = read_name(offset, data)
- log.debug('PTR: %s' % name)
- y['mdns'] = name
- answers.append(name.replace('._pdl-datastream._tcp.local.', ''))
- continue
- '.'.join
- if info[0] == QTYPE_TXT:
- (offset, name) = read_data(offset, data, info[3])
- txt = { }
- off = 0
- while off < len(name):
- l = ord(name[off])
- off += 1
- result = name[off:off + l]
-
- try:
- (key, value) = result.split('=')
- txt[key] = value
- except ValueError:
- read_data_unpack(offset, data, '!HH')
- read_data_unpack(offset, data, '!HH')
- read_data_unpack(offset, data, '!HHHHHH')
- pas
- except:
- read_data_unpack(offset, data, '!HH')
-
- off += l
- continue
- read_data_unpack(offset, data, '!HH')
- log.debug('TXT: %s' % repr(txt))
- y['device1'] = 'MFG:Hewlett-Packard;MDL:%s;CLS:PRINTER;' % txt['ty']
- if 'note' in txt:
- y['note'] = txt['note']
-
- 'note' in txt
- if info[0] == QTYPE_SRV:
- (priority, weight, port) = (offset,)
- ttl = info[3]
- (offset, server) = read_name(offset, data)
- log.debug('SRV: %s TTL=%d PRI=%d WT=%d PORT=%d' % (server, ttl, priority, weight, port))
- y['hn'] = server.replace('.local.', '')
- continue
- read_data_unpack(offset, data, '!HHH')
- if info[0] == QTYPE_AAAA:
- (offset, result) = read_data(offset, data, 16)
- log.debug('AAAA: %s' % repr(result))
- continue
- read_data_unpack(offset, data, '!HHHHHH')
- log.error('Unknown DNS record type (%d).' % info[0])
-
-
- found_devices[y['ip']] = y
- log.debug('Found %d devices' % len(found_devices))
- return found_devices
-
-